package com.clear.subject.domain.handler.subject;

import com.alibaba.fastjson.JSON;
import com.clear.subject.common.enums.IsDeletedFlagEnum;
import com.clear.subject.common.enums.SubjectInfoTypeEnum;
import com.clear.subject.domain.convert.SubjectMultipleConverter;
import com.clear.subject.domain.convert.SubjectRadioConverter;
import com.clear.subject.domain.entity.SubjectAnswerBO;
import com.clear.subject.domain.entity.SubjectInfoBO;
import com.clear.subject.domain.entity.SubjectOptionBO;
import com.clear.subject.infra.basic.entity.SubjectMultiple;
import com.clear.subject.infra.basic.entity.SubjectRadio;
import com.clear.subject.infra.basic.service.SubjectRadioService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 单选题目的策略类
 */
@Component
@Slf4j
public class RadioTypeHandler implements SubjectTypeHandler {

    @Resource
    private SubjectRadioService subjectRadioService;

    /**
     * 枚举身份的识别
     *
     * @return
     */
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.RADIO;
    }

    /**
     * 实际的题目的插入
     *
     * @param subjectInfoBO
     */
    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        // ====单选题目的插入=====
        try {
            if (log.isInfoEnabled()) {
                log.info("RadioTypeHandler.add.bo:{}", JSON.toJSONString(subjectInfoBO));
            }
            // fixme OptionList未做非空校验
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoBO.getOptionList()), "题目选项不能为空");

            List<SubjectRadio> subjectRadioList = new LinkedList<>();   // 单选题的选项有好多个，创建一个容器，进行批量插入

            subjectInfoBO.getOptionList().forEach(option -> {   // 遍历所有选项，并add到容器中
                SubjectRadio radio = SubjectRadioConverter.INSTANCE.convertBOToEntity(option);
                // fixme
                radio.setSubjectId(subjectInfoBO.getId()); // 为每个选择设置 subject_id
                radio.setIsDeleted(IsDeletedFlagEnum.UN_DELETE.getCode());

                subjectRadioList.add(radio);
            });

            subjectRadioService.batchInsert(subjectRadioList);
        } catch (Exception e) {
            log.error("RadioTypeHandler.add.error:{}", e.getMessage(), e);
        }
    }

    /**
     * 查询题目详情
     *
     * @param subjectId 题目id
     */
    @Override
    public SubjectOptionBO query(int subjectId) {
        SubjectRadio subjectRadio = new SubjectRadio();
        subjectRadio.setSubjectId(Long.valueOf(subjectId));

        // 根据题目id去查
        List<SubjectRadio> result = subjectRadioService.queryByCondition(subjectRadio);

        List<SubjectAnswerBO> subjectAnswerBOList = SubjectRadioConverter.INSTANCE.convertEntityToBoList(result);

        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOList);

        return subjectOptionBO;
    }
}
